package ysq.xyg.shop.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import ysq.xyg.base.aspect.BaseLog;
import ysq.xyg.base.controller.BaseController;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.BaseUtil;
import ysq.xyg.base.util.EasyPoiUtil;
import ysq.xyg.shop.model.po.ShopWithdraw;
import ysq.xyg.shop.model.vo.ShopWithdrawExportVo;
import ysq.xyg.shop.model.vo.ShopWithdrawVo;
import ysq.xyg.shop.service.ShopWithdrawService;
import ysq.xyg.web.model.po.WebMember;
import ysq.xyg.web.service.WebMemberService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 商城模块-提现表controller层
 *
 * @author congcong tao
 * @date 2019-4-28 11:19:58
 */
@RestController
public class ShopWithdrawController extends BaseController {

    private static Logger log= LoggerFactory.getLogger(ShopWithdrawController.class);
    @Autowired
    private WebMemberService webMemberService;
    @Resource
    private ShopWithdrawService shopWithdrawService;

    private ShopWithdraw shopWithdraw;

    /**
     * 分页查询
     *
     * @param shopWithdrawVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @author congcong tao
     * @date 2019-4-28 11:19:58
     */
    @BaseLog(module = "商城模块",className = "提现表",methods = "分页查询",businessType = BaseConstant.SYS_BUSINESS_TYPE_GET,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/shopWithdraw/page",method = RequestMethod.POST)
    public ReturnEntity page(ShopWithdrawVo shopWithdrawVo, int pageSize, int pageNum){
        ReturnEntity returnEntity = shopWithdrawService.pageLeft(shopWithdrawVo,pageSize,pageNum);
        return returnEntity;
    }

    /**
     * 跳转连接
     *
     * @param id 主键id
     * @author congcong tao
     * @date 2019-4-28 11:19:58
     */
    @RequestMapping(value = "/shopWithdraw/getEntity",method = RequestMethod.GET)
    public ModelAndView getEntity(HttpServletRequest request, String id){
        ModelAndView view = new ModelAndView("/shop/shopWithdrawUpdateOrSave");
        if(StrUtil.isNotEmpty(id)){
            shopWithdraw = shopWithdrawService.selectByPrimaryKey(id);
            String memberId = shopWithdraw.getMemberId();
            WebMember webMember = webMemberService.selectByPrimaryKey(memberId);
            if (webMember != null) {
                //设置会员昵称
                if (StringUtil.isNotEmpty(webMember.getNickName())) {
                    webMember.setNickName(BaseUtil.decodeBase64(webMember.getNickName()));
                }
            }
            view.addObject("shopWithdraw", shopWithdraw);
            view.addObject("webMember", webMember);
        }
        return view;
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @author congcong tao
     * @date 2019-4-28 11:19:58
     */
    @BaseLog(module = "商城模块",className = "提现表",methods = "删除",businessType = BaseConstant.SYS_BUSINESS_TYPE_DELETE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/shopWithdraw/delete")
    public ReturnEntity delete(HttpServletRequest request,String id){
        return shopWithdrawService.delete(id,acceptCurrentUser(request));
    }

    /**
     * 提现状态，如果否的话需要将该金额还给用户
     * @param id
     * @param status
     * @author congcong tao
     * @date 2019/8/6 17:22
     */
    @RequestMapping(value = "/shopWithdraw/updateStatus")
    public ReturnEntity updateStatus(String id, String status, String description){
        return shopWithdrawService.updateStatus(id, status, description);
    }

    /**
     * 添加或修改
     *
     * @param shopWithdrawVo Vo对象
     * @author congcong tao
     * @date 2019-4-28 11:19:58
     */
    @BaseLog(module = "商城模块",className = "提现表",methods = "添加或修改",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping(value = "/shopWithdraw/addOrUpdate",method = RequestMethod.POST)
    public ReturnEntity delete(HttpServletRequest request,ShopWithdrawVo shopWithdrawVo){
        return shopWithdrawService.addOrUpdate(shopWithdrawVo,acceptCurrentUser(request));
    }

    /**
     * 导出
     * @param response
     * @param shopWithdrawVo
     */
    @BaseLog(module = "商城模块",className = "提现表",methods = "导出",businessType = BaseConstant.SYS_BUSINESS_TYPE_UPDATE,operatorType= BaseConstant.SYS_OPERATOR_TYPE_PC)
    @RequestMapping("/shopWithdraw/export")
    public void export(HttpServletResponse response, ShopWithdrawVo shopWithdrawVo){
        if(StringUtil.isNotEmpty(shopWithdrawVo.getMemberNickName())){
            shopWithdrawVo.setMemberNickName(BaseUtil.encodeBase64String(shopWithdrawVo.getMemberNickName()));
        }
        List<ShopWithdraw> shopWithdrawList = shopWithdrawService.selectEntityList(shopWithdrawVo);
        List<ShopWithdrawExportVo> shopWithdrawExportVoList = new ArrayList<ShopWithdrawExportVo>();
        if (shopWithdrawList != null && shopWithdrawList.size() > 0) {
            for (ShopWithdraw shopWithdraw : shopWithdrawList) {
                ShopWithdrawExportVo shopWithdrawExportVo = new ShopWithdrawExportVo();
                String nickName = "";
                if (StringUtils.isNotEmpty(shopWithdraw.getMemberNickName())) {
                    nickName = BaseUtil.decodeBase64(shopWithdraw.getMemberNickName());
                }
                BeanUtil.copyProperties(shopWithdraw, shopWithdrawExportVo);
                shopWithdrawExportVo.setMemberNickName(nickName);
                // bean复制
                shopWithdrawExportVoList.add(shopWithdrawExportVo);
            }
        }
        String nowDate = DateUtil.formatDate(DateUtil.date());
        // 导出操作
        EasyPoiUtil.exportExcel(shopWithdrawExportVoList, nowDate + "提现展示", "订单", ShopWithdrawExportVo.class, nowDate + "提现展示.xls", response);
    }
}